// pages/common/recruitment-detail/bottom-info/index.js
import {deleteItem, addDeliver} from "../../../../service/Position";
import {getResumeList} from "../../../../service/Student";
import {addCollect, delFocus} from "../../../../service/Collection";
import {getStorageSync} from "../../../../utils/Storage";
import Notification from "../../../../utils/Notification";
import Config from "../../../../main.config";
import Toast from "@vant/weapp/toast/toast";
import Dialog from "@vant/weapp/dialog/dialog";
import {sendGZHTemplate} from "../../../../service/Message";
Component({

    /*
    * 默认接收全局样式
    * */
    options: {
        addGlobalClass: true
    },

    /**
     * 组件的属性列表
     */
    properties: {
        name: {
            type: String,
            value: ""
        },
        info: {
            type: Object,
            value: {}
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        _isShowEdit: false,
        _isDeliveryLoading: false,
        _action: {
            show: false,
            list: []
        }
    },

    observers: {
        "info": function (val) {
            if (val && Object.keys(val).length > 0) {
                let userId = getStorageSync(Config.storage.userInfo + ".userId");
                this.setData({
                    _isShowEdit: userId === val.createUser
                })
            }
        }
    },

    lifetimes: {
        attached:  function () {
            let userInfo = getStorageSync(Config.storage.userInfo);
            this.data._registerInfo = getStorageSync(Config.storage.registerInfo);
            this.setData({roleId: userInfo.roleId})
            this.getResumeList();
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {

        /*
        * 获取简历列表
        * */
        getResumeList: async function () {
            try {
                let registerInfo = this.data._registerInfo;
                let res = await getResumeList({
                    page: 1,
                    limit: 20,
                    createUser: registerInfo.createUser
                });
                let list = res.data || [];
                this.setData({
                    "_action.list": list.map(function (item) {
                        return {
                            name: item.name,
                            path: item.path
                        }
                    })
                })
            } catch (e) {
                console.error(e)
            }
        },

        /*
        * 默认按钮点击
        * 点击按钮
        * */
        onDefaultButtonClick: async function (e) {
            try {
                let detail = this.data.info;
                let name = e.currentTarget.dataset.name;
                if (name === "delete") {
                    await Dialog.confirm({
                        title: "提示",
                        message: "确认删除该条招聘信息吗"
                    })
                    Toast.loading({
                        message: "删除中...",
                        mask: true,
                        duration: 0
                    });
                    await deleteItem(detail.id);
                    Toast.success("删除成功！");
                    setTimeout(function () {
                        Notification.post({name: Config.notification.deleteRecruitment})
                        wx.navigateBack({delta: 2});
                    }, 1000)
                } else { //编辑
                    wx.navigateTo({
                        url: "/pages/common/recruitment-release/index?todo=edit&id=" + detail.id,
                        events: {
                            acceptDataFromOpenedPage: function(data) {
                                wx.navigateBack({delta: 2});
                            }
                        }
                    })
                }
            } catch (e) {
                Toast.clear();
                e.errCode ? Toast.fail("删除失败") : null;
                console.error(e)
            }
        },

        /*
        * 收藏
        * */
        onCollect: async function () {
            try {
                let id = this.data.info.id;
                let registerInfo = this.data._registerInfo;
                let collection = this.data.info.collection;
                if (collection > 0) {
                    await delFocus({
                        userId: registerInfo.id,
                        objectId: id
                    })
                    this.setData({"info.collection": 0});
                    Toast("已取消");
                } else {
                    await addCollect({
                        userId: registerInfo.id,
                        objectId: id
                    })
                    this.setData({"info.collection": 1});
                    Toast("已收藏");
                }
            } catch (e) {
                this.setData({"info.collection": 0});
                Toast.fail("操作失败");
                console.error(e)
            }
        },

        /*
        * 投递
        * */
        onDeliveryButton: async function () {
            try {
                const that = this;
                let resumeList = this.data._action.list;
                if (resumeList.length === 0) {
                    await Dialog.confirm({
                        title: "提示",
                        message: "您还没有添加简历，请先添加简历"
                    })
                    wx.navigateTo({
                        url: "/packageStudent/pages/resume-add/index",
                        events: {
                            acceptDataFromOpenedPage: function() {
                                that.getResumeList()
                            }
                        },
                    })
                    return false
                } else {
                    this.setData({"_action.show": true})
                }
            } catch (e) {
                console.log("取消")
            }
        },

        /*
        * 选择弹框选项
        * */
        onActionSelect: async function (e) {
            try {
                let info = this.data.info;
                let path = e.detail.path;
                let isDeliveryLoading = this.data._isDeliveryLoading;
                if (isDeliveryLoading) return false;
                this.setData({_isDeliveryLoading: true})
                await addDeliver({
                    staffId: info.staffId,
                    positionId: info.id,
                    companyId: info.companyId,
                    resume: path
                })
                Toast.success("投递成功！");
                this.setData({"info.rdCount": 1});
                await sendGZHTemplate({
                    userId: info.createUser,
                    companyId: info.companyId,
                    pagepath: "/pages/index/index?active=1",
                    type: 4
                })
            } catch (e) {
                console.error(e)
            } finally {
                this.setData({_isDeliveryLoading: false})
            }
        },

        /*
        * 关闭弹框
        * */
        onActionClose: function () {
            this.setData({"_action.show": false})
        }
    }
})
